package com.pfzy.lib.net.exception;

import android.net.ParseException;
import android.text.TextUtils;
import android.util.MalformedJsonException;

import com.google.gson.JsonParseException;
import com.pfzy.lib.net.BaseEntity;
import com.pfzy.lib.net.ServiceManager;
import com.pfzy.lib.sdk.log.UXLog;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import javax.net.ssl.SSLHandshakeException;

import retrofit2.HttpException;

public class ExceptionHandler {
    private static IExceptionHandler mExceptionHandler;
    public static IExceptionInterceptor mExceptionInterceptor;

    public static class ERROR {
        public static final int UNKNOWN = -1000;
        public static final int PARSE_ERROR = -1001;
        public static final int NETWORK_ERROR = -1002;
        public static final int SSL_ERROR = -1005;
        public static final int TIMEOUT_ERROR = -1006;
        public static final int ILLEGAL_STATE_ERROR = -1007;
        public static String UNKNOWN_MESSAGE = "网络异常";
        public static String PARSE_ERROR_MESSAGE = "json解析错误";
        public static String NETWORK_ERROR_MESSAGE = "连接失败";
        public static String SSL_ERROR_MESSAGE = "证书验证失败";
        public static String TIMEOUT_ERROR_MESSAGE = "连接超时";
        public static String ILLEGAL_STATE_ERROR_MESSAGE = "状态不合法";
    }

    public static void setExceptionHandler(IExceptionHandler exceptionHandler) {
        mExceptionHandler = exceptionHandler;
    }

    public static CCHttpException handleHttpException(Throwable e) {
        int code;
        String message;
        CCHttpException ccHttpException;
        UXLog.e("HTTP--ExceptionHandler", e.toString());
        e.printStackTrace();

        if (mExceptionHandler != null) {
            return mExceptionHandler.handleHttpException(e);
        }

        if (mExceptionInterceptor == null || (ccHttpException = mExceptionInterceptor.onHttpException(e)) == null) {
            if (e instanceof HttpException) {
                HttpException httpException = (HttpException) e;
                code = httpException.code();
                message = httpException.message();
                if (TextUtils.isEmpty(message)) {
                    message = ERROR.UNKNOWN_MESSAGE;
                }
            } else if ((e instanceof ConnectException) || (e instanceof UnknownHostException)) {
                code = ERROR.NETWORK_ERROR;
                message = ERROR.NETWORK_ERROR_MESSAGE;
            } else if (e instanceof SSLHandshakeException) {
                code = ERROR.SSL_ERROR;
                message = ERROR.SSL_ERROR_MESSAGE;
            } else if ((e instanceof ConnectTimeoutException) || (e instanceof SocketTimeoutException)) {
                code = ERROR.TIMEOUT_ERROR;
                message = ERROR.TIMEOUT_ERROR_MESSAGE;
            } else if (e instanceof IllegalStateException) {
                code = ERROR.ILLEGAL_STATE_ERROR;
                message = ERROR.ILLEGAL_STATE_ERROR_MESSAGE;
            } else if (ServiceManager.mDebugMode) {
                throw new RuntimeException(e);
            } else {
                if ((e instanceof JsonParseException) || (e instanceof JSONException) || (e instanceof ParseException) || (e instanceof MalformedJsonException) || (e instanceof ClassCastException)) {
                    code = ERROR.PARSE_ERROR;
                    message = ERROR.PARSE_ERROR_MESSAGE + e.getMessage();
                } else {
                    code = ERROR.UNKNOWN;
                    message = ERROR.UNKNOWN_MESSAGE;
                }
            }
            return new CCHttpException(code, message);
        }
        return ccHttpException;
    }

    public static CCHttpException handleBizException(BaseEntity baseEntity) {
        CCHttpException ccHttpException;
        if (mExceptionHandler != null) {
            return mExceptionHandler.handleBizException(baseEntity);
        }
        return (mExceptionInterceptor == null || (ccHttpException = mExceptionInterceptor.onBizException(baseEntity)) == null) ? new CCHttpException(baseEntity.code, baseEntity.message) : ccHttpException;
    }
}
